using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class Station : EveObject
    {
        private int security;
        private double dockingCostPerVolume;
        private double maxShipVolumeDockable;
        private int officeRentalCost;
        private int operationId;
        private int stationTypeId;
        private int corporationId;
        private int solarSystemId;
        private int constellationId;
        private int regionId;
        private string stationName;
        private double x, y, z;
        private double reprocessingEfficiency;
        private double reprocessingStationsTake;
        private double reprocessingHangarFlag;
        private DateTime capitalStation; // rev 2.0 only
        private DateTime ownershipDateTime; // rev 2.0 only
        private int upgradeLevel; // rev 2.0 only
        private int customServiceMask; // rev 2.0 only

        private Operation operation;
        private StationType stationType;
        private Corporation corporation;
        private SolarSystem solarSystem;
        private Constellation constellation;
        private Region region;
        private Vector location;

        private Dictionary<AssemblyLineType, AssemblyLineStation> assemblyLines;

        public int Security 
        { 
            get { return security; } 
        }
        public double DockingCostPerVolume 
        { 
            get { return dockingCostPerVolume; } 
        }
        public double MaxShipVolumeDockable 
        { 
            get { return maxShipVolumeDockable; } 
        }
        public int OfficeRentalCost 
        { 
            get { return officeRentalCost; } 
        }
        public Operation Operation
        {
            get
            {
                if (operation == null)
                    operation = new Operation(db, operationId);
                return operation;
            }
        }
        public StationType StationType
        {
            get
            {
                if (stationType == null)
                    stationType = new StationType(db, stationTypeId);
                return stationType;
            }
        }
        public Corporation Corporation
        {
            get
            {
                if (corporation == null)
                    corporation = new Corporation(db, corporationId);
                return corporation;
            }
        }
        public SolarSystem SolarSystem
        {
            get
            {
                if (solarSystem == null)
                    solarSystem = new SolarSystem(db, solarSystemId);
                return solarSystem;
            }
        }
        public Constellation Constellation
        {
            get
            {
                if (constellation == null)
                    constellation = new Constellation(db, constellationId);
                return constellation;
            }
        }
        public Region Region
        {
            get
            {
                if (region == null)
                    region = new Region(db, regionId);
                return region;
            }
        }
        public string Name 
        { 
            get { return stationName; } 
        }
        public Vector Location
        {
            get
            {
                if (location == null)
                    location = new Vector(x, y, z);
                return location;
            }
        }
        public double ReprocessingEfficiency 
        { 
            get { return reprocessingEfficiency; } 
        }
        public double ReprocessingStationsTake 
        { 
            get { return reprocessingStationsTake; } 
        }
        public double ReprocessingHangarFlag 
        { 
            get { return reprocessingHangarFlag; } 
        }
        public Dictionary<AssemblyLineType, AssemblyLineStation> AssemblyLines
        {
            get
            {
                if (assemblyLines == null)
                {
                    assemblyLines = new Dictionary<AssemblyLineType, AssemblyLineStation>();
                    string query = "SELECT * FROM ramAssemblyLineStations WHERE stationID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                    {
                        AssemblyLineStation als = new AssemblyLineStation(row, this);
                        assemblyLines[als.AssemblyLineType] = als;
                    }
                }
                return assemblyLines;
            }
        }
        public DateTime CapitalStation // rev 2.0 only
        {
            get { return capitalStation; }
        }
        public DateTime OwnershipDateTime // rev 2.0 only
        {
            get { return ownershipDateTime; }
        }
        public int UpgradeLevel // rev 2.0 only
        {
            get { return upgradeLevel; }
        }
        public int CustomServiceMask // rev 2.0 only
        {
            get { return customServiceMask; }
        }

        internal Station(DbDataRow dr)
            : base(dr.Database, EveObjectType.Station, dr["stationID"])
        {
            Fill(dr);
        }
        internal Station(Database db, int id)
            : base(db, EveObjectType.Station, id)
        {
            Fill(db.ExecuteSingle("SELECT * FROM staStations WHERE stationID=" + id));
        }

        private void Fill(DbDataRow dr)
        {
            security = dr["security"];
            dockingCostPerVolume = dr["dockingCostPerVolume"];
            maxShipVolumeDockable = dr["maxShipVolumeDockable"];
            officeRentalCost = dr["officeRentalCost"];
            operationId = dr["operationID"];
            stationTypeId = dr["stationTypeID"];
            corporationId = dr["corporationID"];
            solarSystemId = dr["solarSystemID"];
            constellationId = dr["constellationID"];
            regionId = dr["regionID"];
            stationName = dr["stationName"];
            x = dr["x"];
            y = dr["y"];
            z = dr["z"];
            reprocessingEfficiency = dr["reprocessingEfficiency"];
            reprocessingStationsTake = dr["reprocessingStationsTake"];
            reprocessingHangarFlag = dr["reprocessingHangarFlag"];
            capitalStation = dr["capitalStation"];
            ownershipDateTime = dr["ownershipDateTime"];
            upgradeLevel = dr["upgradeLevel"];
            customServiceMask = dr["customServiceMask"];
        }
    }
}
